package com.uphone.magicleafteacher.pro.activity.listenertrainscore;

import android.arch.lifecycle.LifecycleOwner;

import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.base.mvp.BasePAV;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RequestBodyUtil;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;

import java.io.File;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by hzy on 2019/1/18
 * WelcomePresenter
 *
 * @author hzy
 */
public class ListenerTrainScorePresenter extends BasePAV<ListenerTrainScoreContract.View> implements ListenerTrainScoreContract.Presenter {

    @Inject
    ListenerTrainScorePresenter() {

    }

    public void giveAMark(Map<String, RequestBody> params, File file) {

        MultipartBody.Part part = RequestBodyUtil.filesToMultipartBodyPart("file", file);
        MyApp.apiService(ApiService.class)
                .giveAMark(params, part)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    mView.showLoading();
                })
                .doFinally(() -> {
                    mView.closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        mView.giveAMark();
                    }
                }, new FailureConsumer());


    }
}
